#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 100005;
ll w[N],l[N],r[N];
int arr[N];
int cnt = 0;
int res = 0;
int n;
void dfs(int i){
    arr[cnt ++] = w[i];
    if(r[i] != 0) dfs(r[i]);
    if(l[i] != 0) dfs(l[i]);
}
int main()
{
    cin >> n;
    for(int i = 1;i <= n;i ++){
        cin >> w[i];
    }
    for(int i = 1;i <= n;i ++){
        cin >> l[i] >> r[i];
    }
    dfs(1);
    int dp[n];
    int ends[n];
    ends[0] = arr[0];
    dp[0] = 1;
    int right = 0;
    int l = 0;
    int r = 0;
    int m = 0;
    for (int i = 1; i < cnt; i++) {
        l = 0;
        r = right;
        while (l <= r) {
            m = (l + r) / 2;
            if (arr[i] > ends[m]) {
                l = m + 1;
            } else {
                r = m - 1;
            }
        }
        right = max(right, l);
        ends[l] = arr[i];
        dp[i] = l + 1;
    }
    for(int i = 0; i< cnt;i ++){
     res = max(res,dp[i]);
    }
    cout << res << endl;
    return 0;
}
